/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.ui.activities;

import java.util.Calendar;

import utsc.apps.utschedule.ui.MyDatePickerDialogFragment;
import utsc.apps.utschedule.ui.MyTimePickerDialogFragment;
import utsc.apps.utschedule.util.database.DatabaseManager;

import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockDialogFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;

/**
 * A generic class for all Fragment activity. It has various helper methods to utilize fragment manager.
 * @author Wenbin
 *
 */
public abstract class GenericFragmentActivity extends SherlockFragmentActivity {
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		//we need to release all the holds to the db helper
		//whenever we leave the current activity
		DatabaseManager.release();
	}
	
	/**
	 * This is a helper method which will unbind all the underlying drawables from all views. 
	 * @param view - the parent view to start from
	 */
	protected void unbindDrawables(View view) {
		if (view!=null) {
			if (view.getBackground() != null) {
		        view.getBackground().setCallback(null);
		        }
		        if (view instanceof ViewGroup) {
		            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
		            unbindDrawables(((ViewGroup) view).getChildAt(i));
		            }
		        ((ViewGroup) view).removeAllViews();
		        }
		}
    }
	
	/**
	 * This is a helper method to remove a fragment by providing the tag name.
	 * @param name -the fragment's tag name
	 * @return the {@link FragmentTransaction} of this operation
	 */
	protected FragmentTransaction removeFragment(String name){
		FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
		Fragment prev=getSupportFragmentManager().findFragmentByTag(name);
		if (prev != null) {       
			ft.remove(prev);
			getSupportFragmentManager().popBackStack(name,FragmentManager.POP_BACK_STACK_INCLUSIVE);
		}
		return ft;
	}
	
	/**
	 * This is a helper method to show a dialog fragment.
	 * @param fragment the given dialog fragment object to show
	 * @param tagName the tag name of the dialog fragment
	 * @return the result from the method call {@code Fragment.show(FragmentTransaction, TagName)}
	 */
	protected int showDialogFragment(SherlockDialogFragment fragment, String tagName) {
		FragmentTransaction ft =removeFragment(tagName);
		return fragment.show(ft, tagName);
		
	}
	
	/**
	 * This is a helper method to show a dialog fragment by using the fragment manager.
	 * @param fragment the given dialog fragment object to show
	 * @param tagName the tag name of the dialog fragment
	 */
	protected void showDialogFragmentByFragmentManager(SherlockDialogFragment fragment, String tagName){
		fragment.show(getSupportFragmentManager(), tagName);
	}
	
	/**
	 * A helper method to show a {@link MyTimePickerDialogFragment Time picker dialog}.
	 * @param currentTimeSelection current selection 
	 * @param is24hour whether or not the time should be displayed in 24 hour-format
	 * @param listener the callback listener
	 */
	protected void showTimePickerDialog(Calendar currentTimeSelection, boolean is24hour, OnTimeSetListener listener){
		if (currentTimeSelection!=null) {
			final int selectedHour=currentTimeSelection.get(Calendar.HOUR_OF_DAY);
			final int selectedMin=currentTimeSelection.get(Calendar.MINUTE);
			MyTimePickerDialogFragment newFragment=MyTimePickerDialogFragment.newInstance(selectedHour, selectedMin, is24hour, listener);  
			showDialogFragmentByFragmentManager(newFragment, "timePickerDialog");
		}
	}
	
	/**
	 * A helper method to show a {@link MyDatePickerDialogFragment Date picker dialog}.
	 * @param currentDateSelection current selection
	 * @param is24hour whether or not the time should be displayed in 24 hour-format
	 * @param listener the callback listener
	 */
	protected void showDatePickerDialog(Calendar currentDateSelection, boolean is24hour, OnDateSetListener listener){
		if (currentDateSelection!=null) {
			final int selectedDay=currentDateSelection.get(Calendar.DATE);
			final int selectedMonth=currentDateSelection.get(Calendar.MONTH);
			final int selectedYear=currentDateSelection.get(Calendar.YEAR);
			MyDatePickerDialogFragment newFragment=MyDatePickerDialogFragment.newInstance(listener, selectedYear, selectedMonth, selectedDay); // selectedHour, selectedMin, is24hour,   
			showDialogFragmentByFragmentManager(newFragment, "datePickerDialog");
		}
	}
	
}
